<?php

namespace App\Api\Controllers;

use App\Http\Controllers\Controller;
use App\Services\Log;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Cache;
use App\Services\OSS;

class CommonController extends Controller
{
    public $uid = 0;
    public $user_info = array();

    public function __construct()
    {
        $langure_code = IT('language_code', 'zh-cn', '参数有误', 10032);
        App::setLocale($langure_code);
    }

    /**
     * 获取用户基本信息
     * @param bool $type  true返回基本信息
     */
    protected function getUserInfo($type=true)
    {
        if ($type === true) {// 是否需要验证是否登录

            $ticket = IT('ticket', true);
            $uid = D('user_tickets')->where([['ticket', '=', $ticket], ['status', '=', 1]])->value('uid');
            if (!$uid)
                EE('用户未登录', 1000, 10004);

            /*$uid = check_ticket($ticket)['uid'];*/
            $this->uid = $uid;        
            // users 字段
            $select = array(
                'id','name','real_name','mobile','avatar',
                'email','sex','isconfirmed','praise','attention','fans',
                'status','created_at','updated_at', 'notice', 'birthday', 'signature'
            );

            $user_info = D('users')->select($select)->where([['id', '=', $uid]])->first();
            if (!$user_info)
                EE('用户不存在', 1000, 10005);

            // 将字符串修改为整形
            $user_info->mobile =  $user_info->mobile ? $user_info->mobile:'';
            $this->user_info = $user_info;
        }
    }

    /**
     * 上传到本地服务器
     * @param BasicRequest $request
     * @return string
     */
    protected function uploadLocalImage($request)
    {
        $file_path = 'images/'.date('Y-m-d').'/';
        $file = $request->file('img_file')->store($file_path);
        $img_url = Storage::url('/'.$file);
        return $img_url;
    }
    /**
     * 上传视频到本地
     * @param $request
     * @return mixed
     */
    protected function uploadLocalVideo($request)
    {
        $file_path = '/videos/'.date('Y-m-d').'/';
        $file = $request->file('video_file')->store($file_path);
        $img_url = Storage::url('/'.$file);
        return $img_url;
    }
}